package com.fumin.controller;

import com.fumin.beans.PageQuery;
import com.fumin.beans.PageResult;
import com.fumin.common.JsonData;
import com.fumin.model.SysUser;
import com.fumin.param.UserParam;
import com.fumin.service.SysRoleService;
import com.fumin.service.SysTreeService;
import com.fumin.service.SysUserService;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Author xiayuxuanmin
 * @Date 2019/10/24
 * @Description
 */
@RequestMapping("/sys/user/")
@Controller
@Slf4j
public class SysUserController {
    @Resource
    private SysUserService sysUserService;
    @Resource
    private SysTreeService sysTreeService;
    @Resource
    private SysRoleService sysRoleService;

    @RequestMapping("/noAuth.page")
    private ModelAndView noAuth(){
        //在视图解析器中，根据prefix和suffix查找对应的JSP文件，这里是查找/WEB-INF下面的/views/noAuth.jsp文件
        return new ModelAndView("noAuth");
    }

    @RequestMapping("/save.json")
    @ResponseBody
    public JsonData saveUser(UserParam param){
        sysUserService.save(param);
        return JsonData.success();
    }

    @RequestMapping("/page.json")
    @ResponseBody
    public JsonData page(@RequestParam("deptId") int deptId, PageQuery pageQuery) {
        PageResult<SysUser> result = sysUserService.getPageByDeptId(deptId, pageQuery);
        return JsonData.success(result);
    }


    @RequestMapping("/update.json")
    @ResponseBody
    public JsonData updateUser(UserParam param){
        sysUserService.update(param);
        return JsonData.success();
    }

    @RequestMapping("acls.json")
    @ResponseBody
    public JsonData acls(@RequestParam("userId") int userId){
        Map<String,Object> map = Maps.newHashMap();
        map.put("acls",sysTreeService.userAclTree(userId));//用户的所有已分配的权限
        map.put("roles",sysRoleService.getRoleListByUserId(userId));//用户的所有已分配的角色
        return JsonData.success(map);
    }
}
